package com.goktech.olala.core.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goktech.olala.core.config.SysConfig;
import com.goktech.olala.core.req.ReqPicture;
import com.goktech.olala.core.resp.RespPictureVo;
import com.goktech.olala.core.service.IPictureService;
import com.goktech.olala.server.dao.goods.GoodsPictureMapper;
import com.goktech.olala.server.pojo.goods.GoodsPicture;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * @author wusq
 * 图片service层
 */
@Service("pictureService")
public class PictureServiceImpl implements IPictureService {

    @Autowired
    GoodsPictureMapper pictureMapper;

    @Override
    public PageInfo<RespPictureVo> queryPictureByParam(ReqPicture reqPicture, int pageNum, int pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<RespPictureVo> respPictureList = pictureMapper.selectByExample(reqPicture);
        PageInfo<RespPictureVo> pageInfo = new PageInfo<>(respPictureList);
        return pageInfo;
    }

    @Override
    public RespPictureVo queryPictureById(Long pictureId) throws Exception {
        if(pictureId == null){
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        RespPictureVo respPicture = new RespPictureVo();
        GoodsPicture picture = pictureMapper.selectByPrimaryKey(pictureId);
        BeanUtils.copyProperties(picture, respPicture);
        if(picture.getDeployTime() != null){
            respPicture.setDeployTime(sdf.format(picture.getDeployTime()));
        }
        if(picture.getOutlineTime() != null){
            respPicture.setOutlineTime(sdf.format(picture.getOutlineTime()));
        }
        if(picture.getUpdateTime() != null){
            respPicture.setUpdateTime(sdf.format(picture.getUpdateTime()));
        }
        return respPicture;
    }

    @Override
    public int removePictureById(Long pictureId) throws Exception {
        if(pictureId == null){
            return 0;
        }
        return pictureMapper.deleteByPrimaryKey(pictureId);
    }

    @Override
    public int removePictureBatch(String pictureIds) throws Exception {
        int result = 0;
        String flag=",";
        if(StringUtils.isNotBlank(pictureIds) && !flag.equals(pictureIds)){
            String[] pictureIdArr = pictureIds.split(flag);
            for (String pictureId : pictureIdArr) {
                if(StringUtils.isBlank(pictureId)){
                    continue;
                }
                result = pictureMapper.deleteByPrimaryKey(Long.valueOf(pictureId));
            }
        }
        return result;
    }

    @Override
    public int savePictureInfo(ReqPicture reqPicture) throws Exception {
        //必须有有效的图片地址
        if(StringUtils.isBlank(reqPicture.getImgPathArr())){
            return 0;
        }
        GoodsPicture goodsPicture = new GoodsPicture();
        String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        BeanUtils.copyProperties(reqPicture, goodsPicture);
        if(StringUtils.isNotBlank(reqPicture.getDeployTime())){
            goodsPicture.setDeployTime(Timestamp.valueOf(reqPicture.getDeployTime()));
        }
        if(StringUtils.isNotBlank(reqPicture.getOutlineTime())){
            goodsPicture.setOutlineTime(Timestamp.valueOf(reqPicture.getOutlineTime()));
        }
        goodsPicture.setRealImg(reqPicture.getImgPathArr());
        goodsPicture.setOriginalImg(reqPicture.getImgPathArr());
        goodsPicture.setThumbImg(reqPicture.getImgPathArr());
        goodsPicture.setUpdateBy(SysConfig.SYSTEM_USER);
        goodsPicture.setUpdateTime(Timestamp.valueOf(nowTime));
        if(reqPicture.getPictureId() != null){
            return pictureMapper.updateByPrimaryKey(goodsPicture);
        }
        goodsPicture.setStatus(0);
        goodsPicture.setIsMaster(1);
        goodsPicture.setCreateBy(SysConfig.SYSTEM_USER);
        goodsPicture.setCreateTime(Timestamp.valueOf(nowTime));
        return pictureMapper.insertByExample(goodsPicture);
    }

    @Override
    public int updatePictureStatus(Long pictureId, int status) throws Exception {
        if(pictureId == null){
            return 0;
        }
        return pictureMapper.updateStatusById(pictureId, status);
    }
}
